package indzcraft.loader;
import java.io.File;
import java.util.HashMap;


public class GemDB {
	
	private HashMap<Integer,Gem> gemDB = new HashMap<Integer,Gem>();
	private IndzCraftLoader loader;
	
	public GemDB(IndzCraftLoader loader){
		this.loader = loader;
		loadFromStorage();
	}
	
	public Gem getGem(int id){
		if(!gemDB.containsKey(id)){
			Gem item = loader.getGemFromWOWHEAD(id);
			if(item != null)
				gemDB.put(item.getId(), item);
		}
		//System.out.println("load from db: "+ id);
		return (Gem) gemDB.get(id);
	}
	
	public HashMap<Integer,Gem> getDB(){
		return gemDB;
	}
	
	public void loadFromStorage(){		
		File file = new File("./gems/");
		if(!file.exists()){
			file.mkdir();
		}
		else if(file.exists()){
			System.out.println("Path /gems/ exists");
			File[] files = file.listFiles();
			for(int i = 0;i< files.length;i++){
				if(files[i].isFile()){
					int gemid = Integer.parseInt(files[i].getName().split("\\.")[0]);
					Gem gem = loader.getGemFromWOWHEAD(gemid);
					if(gem != null)
						gemDB.put(gem.getId(), gem);
				}
			}
		}
		System.out.println("directory gems imported");
	}
}
